package com.heima.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @Auth: zhuan
 * @Desc: Activiti第四步：流程任务历史
 * @Date: 2022-03-25 18:50:37
 */
@SpringBootTest(classes = SpringbootActivity7Application.class)
@RunWith(SpringRunner.class)
public class ActivitiHistoryTask004 {
		/**
		 *  HistoryService：Activiti的历史管理类，可以查询历史信息、流程实例启动时间、任务的参与者、完成任务的时间、每个流程实例的执行路径等等
		 */
		@Autowired
		private HistoryService historyService;

		//根据用户名查询历史记录
		/**
		 * 根据用户名查询历史记录--包含所有流程：未完成、已完成
		 */
		@Test
		public void HistoricTaskInstanceByUser(){
				List<HistoricTaskInstance> list = historyService
												.createHistoricTaskInstanceQuery()
												.orderByHistoricTaskInstanceEndTime().asc()
												.taskAssignee("heima")
												.list();
				for(HistoricTaskInstance hi : list){
						System.out.println("Id："+ hi.getId());
						System.out.println("ProcessInstanceId："+ hi.getProcessInstanceId());
						System.out.println("Name："+ hi.getName());
						System.out.println("CreateTime："+ hi.getCreateTime());
						System.out.println("EndTime："+ hi.getEndTime());
						System.out.println("WorkTime："+ hi.getWorkTimeInMillis());
						System.out.println("Duration："+ hi.getDurationInMillis());
						System.out.println("-----------------------------------------------------");
				}

		}
		/**
		 * 根据流程实例ID查询历史，特别注意是【流程实例ID】而非【任务ID】
		 */
		@Test
		public void HistoricTaskInstanceByPiID(){
				List<HistoricTaskInstance> list = historyService
												.createHistoricTaskInstanceQuery()
												.orderByHistoricTaskInstanceEndTime().asc()
												.processInstanceId("91c1218c-ac2a-11ec-80fb-005056c00008")
												.list();
				for(HistoricTaskInstance hi : list){
						System.out.println("Id："+ hi.getId());
						System.out.println("ProcessInstanceId："+ hi.getProcessInstanceId());
						System.out.println("Name："+ hi.getName());
						System.out.println("CreateTime："+ hi.getCreateTime());
						System.out.println("EndTime："+ hi.getEndTime());
						System.out.println("WorkTime："+ hi.getWorkTimeInMillis());
						System.out.println("Duration："+ hi.getDurationInMillis());
						System.out.println("-----------------------------------------------------");
				}
		}
}
